package com.chenyuhan.myview.testview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.chenyuhan.myview.R;

import static android.R.attr.radius;

/**
 * Created by Administrator on 2017/6/28 0028.
 */

public class TestLoadingView extends View {
    private final String TAG = TestLoadingView.class.getSimpleName();

    /**
     * 第一层圆圈的颜色
     */
    private int mFirstColor;
    /**
     * 第二层圆圈的颜色
     */
    private int mSecondColor;
    /**
     * 圆圈的宽度
     */
    private int mCircleWidth;
    /**
     * 速度
     */
    private int mSpeed;
    /**
     * 进度
     */
    private int mProgress;
    /**
     * 进度文字
     */
    private String mProgressText;
    /**
     * 进度文字颜色
     */
    private int mProgressTextColor;
    /**
     * 进度文字大小
     */
    private int mProgressTextSize;
    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 是否画下一个圆环
     */
    private boolean isNext;
    private Rect mBound;

    public TestLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSet();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TestLoadingView, defStyleAttr, 0);
        for (int i = 0; i < typedArray.getIndexCount(); i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.TestLoadingView_firstColor:
                    //默认颜色设置为黑色
                    mFirstColor = typedArray.getInt(attr, Color.BLACK);
                    break;
                case R.styleable.TestLoadingView_secondColor:
                    // 默认颜色设置为白色
                    mSecondColor = typedArray.getColor(attr, Color.WHITE);
                    break;
                case R.styleable.TestLoadingView_circleWidth:
                    //设置圆圈的宽度
                    mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.TestLoadingView_speed:
                    mSpeed = typedArray.getInt(attr, 10);//默认10
                    break;
                case R.styleable.TestLoadingView_progressText:
                    mProgressText = typedArray.getString(attr);
                    break;
                case R.styleable.TestLoadingView_progressTextColor:
                    mProgressTextColor = typedArray.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.TestLoadingView_progressTextSize:
                    // 默认设置为16sp，TypeValue也可以把sp转化为px
                    mProgressTextSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        typedArray.recycle();
        mPaint = new Paint();
        // 设置线程
        new Thread() {
            public void run() {
                while (true) {
                    mProgress++;
                    if (mProgress == 360) {
                        mProgress = 0;
                        isNext = !isNext;
                    }
                    mProgressText = String.valueOf(100 * mProgress / 360) + "/100";
                    postInvalidate();
                    try {
                        Thread.sleep(mSpeed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    /**
     * 初始化默认值
     */
    private void initSet() {
        mFirstColor = Color.BLACK;
        mSecondColor = Color.WHITE;
        mCircleWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics());
        mSpeed = 10;
        mProgress = 0;
        mProgressText = "0/100";
        mProgressTextColor = Color.BLACK;
        mProgressTextSize = mCircleWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, 16, getResources().getDisplayMetrics());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setStrokeWidth(mCircleWidth);// 设置圆环的宽度
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        //外圆半径，假设centerX=150，而mCircleWidth=30，则代表我们要画一个半径为150-30=120的圆，
        //但由于画笔有宽度，且宽度=30，画笔在画时是从中心画笔的中心画起，画出的圆线中心为120的圆，左右各多15宽度
        //所以此时画出的圆的宽度为150-30-15=105，不符合要求，所以我们要将而mCircleWidth/2，画外圆的时候从半径150-30/2=135开始画，
        //此时由于画笔宽度问题，所以实际画出的圆的内部圆的半径为150-15-15=120，符合要求
        int centerX = getWidth() / 2; //中心X坐标
        int centerY = getHeight() / 2; //中心X坐标
        int r = centerX - mCircleWidth / 2;//中心圆半径

        RectF rectF = new RectF(centerX - r, centerY - r, centerX + r, centerY + r);
        if (!isNext) {
            mPaint.setColor(mFirstColor);//设置圆环颜色
            //参数（从左到右）：圆心的x坐标;圆心的y坐标;圆的半径;绘制时所使用的画笔
            canvas.drawCircle(centerX, centerY, r, mPaint);//画出圆环
            mPaint.setColor(mSecondColor); // 设置圆环的颜色
            //参数（从左到右）：圆弧所在的椭圆对象;圆弧的起始角度;圆弧的角度;是否显示半径连线，true表示显示圆弧与圆心的半径连线，false表示不显示;绘制时所使用的画笔
            canvas.drawArc(rectF, -90, mProgress, false, mPaint); // 根据进度画圆弧
        } else {
            mPaint.setColor(mSecondColor); // 设置圆环的颜色
            canvas.drawCircle(centerX, centerY, radius, mPaint); // 画出圆环
            mPaint.setColor(mFirstColor); // 设置圆环的颜色
            canvas.drawArc(rectF, -90, mProgress, false, mPaint); // 根据进度画圆弧
        }
        //画进度
        mBound = new Rect();
        if (mProgressTextSize != 0)
            mPaint.setTextSize(mProgressTextSize);
        if (mProgressText != null)
            // 计算了描绘字体需要的范围
            mPaint.getTextBounds(mProgressText, 0, mProgressText.length(), mBound);

        mPaint.setStrokeWidth(1);//设置画笔宽度颜色实心
        mPaint.setColor(mProgressTextColor);
        mPaint.setStyle(Paint.Style.FILL);
        //计算文本最左边和底边的坐标
        int textX = centerX - mBound.right / 2;
        int textY = centerY + mBound.height() / 2;
        canvas.drawText(mProgressText, textX, textY, mPaint);
    }
}
